import { GivePriceData } from "../全局对象/ItemManage";
import { getItemMangae } from "../系统功能/GlobalObjectManage";
import { getMoneyName } from "../配置/基础配置";


export function Main(Npc: TNormNpc, Player: TPlayObject, Args: TArgs) {

    let ItemsString = `<{S=自定回收设置;C=251;X=0;Y=0}/@回收.自动回收设置>`;
    let list = getItemMangae().getAllLv();
    let y = 0
    for (let i = 0; i < list.length; i++) {
        let name = getItemMangae().getLvNameByIndex(i);
        let price: GivePriceData = getItemMangae().getPriceByLvIndex(i);
        let moneyName: string = getMoneyName(price.moneyType);
        y += 15
        ItemsString += `<{S=${name}可回收${moneyName}${price.price}个;C=250;X=0;Y=${y}}>`
        ItemsString += `<{S=一键回收;C=251;X=250;Y=${y}}/@回收.回收(${i})>`
    }
    Npc.Say(Player, ItemsString)
}

export function 回收(Npc: TNormNpc, Player: TPlayObject, Args: TArgs) {
    let index: number = Args.Int[0];
    let price: GivePriceData = getItemMangae().getPriceByLvIndex(index);
    let list: Array<String> = getItemMangae().getItemLvList2(index);
    let num = 0;
    for (let i = Player.MaxBagSize - 1; i >= 0; i--) //循环背包
    {
        let AItem: TUserItem = Player.GetBagItem(i); //取出一个背包物品 
        if (AItem) //如果物品存在
        {
            if (list.includes(AItem.Name)) {
                if (Player.CanAddGameMoney(price.moneyType, price.price)) {
                    Npc.TakeItem(Player, AItem); //收走物品 
                    Player.AddGameMoney(price.moneyType, price.price, '回收')
                    num += price.price;
                }
                else {
                    Player.MessageBox('货币已满  无法回收');
                    break
                }

            }

        }

    }
    if (num > 0) {
        let moneyName = getMoneyName(price.moneyType);
        Player.SendMessage(`成功回收物品,获得${moneyName}${num}个`, 1);
    }

}
export function 自动回收设置(Npc: TNormNpc, Player: TPlayObject, Args: TArgs) {
    let y = 0
    let ItemsString = ``;
    let list = getItemMangae().getAllLv();

    for (let i = 0; i < list.length; i++) {
        let name = getItemMangae().getLvNameByIndex(i);
        let checkKey = getItemMangae().getRecycleKey(i);
        let check = Player.GetCheck(checkKey);
        ItemsString += `<{S=自动回收${name};C=255;X=0;Y=${y}}><{S=${check ? '已开启' : '已关闭'};C=251;Y=${y}}>`
        ItemsString += `<{S=${check ? '关闭' : '打开'};C=251;X=250;Y=${y}}/@回收.切换开关(${checkKey})>`
        y += 15
    }
    Npc.Say(Player, ItemsString)


}

export function 切换开关(Npc: TNormNpc, Player: TPlayObject, Args: TArgs) {
    let index: number = Args.Int[0];

    let flag = !Player.GetCheck(index)
    Player.SetCheck(index, flag);
    自动回收设置(Npc, Player, Args)
}